package com.example.controller;

import com.example.exception.BusinessException;
import com.example.exception.SystemException;
import com.example.result.ReturnCode;
import com.example.result.ReturnResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionAdvice {

    /*
     系统异常
     */
    @ExceptionHandler(SystemException.class)
    public ReturnResult systemException(SystemException systemException){
        return new ReturnResult(systemException.getCode(), systemException.getMessage(),null);
    }

    /*
      业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public ReturnResult businessException(BusinessException businessException){
        return new ReturnResult(businessException.getCode(),businessException.getMessage(),null);
    }

    /*
      其他异常
     */
    @ExceptionHandler(Exception.class)
    public ReturnResult otherException(Exception exception){
        System.out.println(exception.getMessage());
        return new ReturnResult(ReturnCode.SYSTEM_UNKNOWN_ERR,exception.getMessage(),null);
    }
}
